/**
 * 
 */
package com.mc.file;

import java.io.File;

import org.apache.commons.io.FileUtils;

import net.coobird.thumbnailator.Thumbnails;

/**
 * @author w.weizhen
 *
 */
public class ImgTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		face();
	}
	
	private static void face()
	{
		Integer size = 1024 * 150;// 目标文件大小，单位kb;
		String path = "D:/hynetic/人脸/";
		File file = new File(path);
		if (file.exists()) 
        {
            File[] files = file.listFiles();
            for (File file2 : files) 
            {
            	if(file2.isFile())
            	{
            		String file2_path = file2.getPath();
            		String file2_name = file2.getName();
            		String file2_target = path + "thumbnailator/" + file2_name;
            		thumbnails(file2_path, file2_target, size);
            		
            	}
            }
        }
	}
	
	
	/**
     * 压缩图片大小至某大小
     * 
     * <一句话功能简述> <br>
     * <功能详细描述>
     * 
     * @param from 路径
     * @param to 路径
     * @param maxSize 文件最大大小
     * @see [类、类#方法、类#成员]
     */
    public static void thumbnails(String from, String to, long maxSize)
    {
        try
        {
            if (from == null || from == "" || !new File(from).isFile())
            {
                return;
            }
            long size = FileUtils.sizeOf(new File(from));
            if (size >= maxSize)
            {
                Thumbnails.of(from).scale(0.9f).toFile(to);// 按比例缩小
                thumbnails(to, to, maxSize);
            }
            else
            {
                Thumbnails.of(from).scale(1f).toFile(to);
                if (FileUtils.sizeOf(new File(to)) >= maxSize)
                {
                    thumbnails(to, to, maxSize);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}